#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_qipan(new qipan(this)),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    /*固定窗口小*/
    setFixedSize(800,680);
    m_login = new GameLogic;
    int boardSize =0;
    m_qipan->getBoardInfo(boardSize);
    m_login->setQipanBoard(m_qipan->getBoardInfo(boardSize),boardSize);
    /*设置布局*/
    QWidget * centerWidget = new QWidget(this);
    setCentralWidget(centerWidget);
    QVBoxLayout * layout = new QVBoxLayout(centerWidget);
    layout->addWidget(m_qipan);
    /*信号和槽*/
    connect(m_qipan,&qipan::chessClick,this,&MainWindow::handleChessClickSlot);
    connect(m_login,&GameLogic::Aiposition,this,&MainWindow::handleAiChessClickSlot);
}


MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::handleChessClickSlot(int row, int col)
{
    m_qipan->setBoardInfo(row,col,PLATER);
    m_qipan->update();
    if(m_login->checkWin(row,col,PLATER)==true)
    {
           QMessageBox::information(this,"游戏结束","玩家胜利");
    }
    else {
        /*AI下棋*/
        /*什么叫最优*/
        /*1.对你自己最好的点+2.对对手最不好的点*/
        m_login->computerMove();
        
    }
}

void MainWindow::handleAiChessClickSlot(int row, int col)
{
    m_qipan->setBoardInfo(row,col,COMPUTER);
    /*更新棋盘*/
    m_qipan->update();
    if(m_login->checkWin(row,col,COMPUTER)==true)
    {
        QMessageBox::information(this,"游戏结束","人机胜利");
    }
}
